package com.example.finalassignment.controller;

import com.example.finalassignment.common.ThreadContext;
import com.example.finalassignment.exception.ServiceException;
import com.example.finalassignment.mapper.LifeMapper;
import com.example.finalassignment.mapper.StarsMapper;
import com.example.finalassignment.mapper.UserMapper;
import com.example.finalassignment.pojo.Life;
import com.example.finalassignment.pojo.Stars;
import com.example.finalassignment.pojo.dto.LifeDto;
import com.example.finalassignment.pojo.response.ResultDataResponse;
import com.example.finalassignment.pojo.response.ResultResponse;
import com.example.finalassignment.util.GenerateUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/life")
public class LifeController {

    private final LifeMapper lifeMapper;
    private final StarsMapper starsMapper;
    private final UserMapper userMapper;

    public LifeController(LifeMapper lifeMapper, StarsMapper starsMapper, UserMapper userMapper) {
        this.lifeMapper = lifeMapper;
        this.starsMapper = starsMapper;
        this.userMapper = userMapper;
    }

    /**
     * 点赞
     * @return
     */
    @GetMapping("/like/{id}")
    public ResultResponse getStarsByLike(@PathVariable("id") String id) {
        String userId = ThreadContext.getUserId();

        try {
            Stars oldStars = starsMapper.selectByPrimaryKey(id);
            if(oldStars == null){
                Stars newStars = new Stars();
                newStars.setUuid(id);
                newStars.setId(0);
                newStars.setUserId(userId);
                newStars.setCreateTime(new Date());
                lifeMapper.updateDraftsByUUIDToStarsAdd(id);
                starsMapper.insertSelective(newStars);
                return ResultResponse.success("点赞成功");
            }else {
                starsMapper.closeByUuidAndUserId(id,userId);
                lifeMapper.updateDraftsByUUIDToStarsCut(id);
                return ResultResponse.success("取消点赞");
            }
        }catch (Exception e){
            throw new ServiceException(201);
        }
    }

    @PostMapping("/insert")
    public ResultResponse insert(@RequestBody LifeDto lifeDto) {
        String title = lifeDto.getTitle();
        String description = lifeDto.getDescription();
        String video = lifeDto.getVideo();

        if (title == null || title.isEmpty()) {
            throw new ServiceException(201, "标题不能为空");
        }

        if (description == null || description.isEmpty()) {
            throw new ServiceException(201, "简述不能为空");
        }

        if (video == null || video.isEmpty()) {
            throw new ServiceException(201, "视频不能为空");
        }

        int idx = lifeDto.getVideo().lastIndexOf("/");

        lifeDto.setVideo(lifeDto.getVideo().substring(idx + 1));

        String userId = ThreadContext.getUserId();
        Life life = new Life();
        life.setId(GenerateUtil.generateUUID());
        life.setStar(0L);
        life.setCreateTime(new Date());
        life.setTitle(lifeDto.getTitle());
        life.setDescription(lifeDto.getDescription());
        life.setStatus(1);
        life.setVideoUuid(lifeDto.getVideo());
        life.setUserId(userId);


        try {
            lifeMapper.insertSelective(life);
            return ResultResponse.success("分享成功");
        } catch (Exception e) {
            throw new ServiceException(201);
        }
    }

    @GetMapping("/list/{value}")
    public ResultDataResponse<List<Map<String, Object>>> getListToVideo(@PathVariable("value") String value){
        if("75395123".equals(value)){
            List<Map<String, Object>> listToVideo = lifeMapper.getListToVideo();

            for(Map<String, Object> map: listToVideo){
                String userId = (String) map.get("user_id");
                Map<String, Object> userInfo = userMapper.selectByIdToUserAndUserInfo(userId);

                map.put("user",userInfo);
                map.remove("user_id");
            }

            return new ResultDataResponse<List<Map<String, Object>>>(listToVideo);
        }

        List<Map<String, Object>> allDataByLike = lifeMapper.getAllDataByLike(value);
        for(Map<String, Object> map: allDataByLike){
            String userId = (String) map.get("user_id");
            Map<String, Object> userInfo = userMapper.selectByIdToUserAndUserInfo(userId);

            map.put("user",userInfo);
            map.remove("user_id");
        }
        return new ResultDataResponse<List<Map<String, Object>>>(allDataByLike);

    }
}
